<?php
namespace app\api\controller\foodcash;

use app\api\model\foodcash\ShopClerk as ShopClerkModel;
use app\BaseController;
use think\response\Json;
use think\facade\Cache;

/**
 * API控制器基类
 */
class Controller extends BaseController
{
    const JSON_SUCCESS_STATUS = 1;
    const JSON_ERROR_STATUS = 0;
    
    
    protected $user; //用户信息
    protected $applet_id; //小程序id
	protected $shop_id; //门店id
    protected $controller = ''; // 当前控制器名称
    protected $action = '';     // 当前方法名称
    protected $routeUri = '';   // 当前路由uri
    protected $group = '';  // 当前路由：分组名称

   //白名单
    protected $allowAllAction = [
        'foodcash.applet/base',
        'foodcash.user/login'
    ];

    /**
     * 基类初始化
     */
    public function initialize()
    {
        $this->applet_id = $this->request->param('applet_id');
        $this->shop_id = $this->request->param('shop_id');
        $this->getUserDetail();// 验证登录
        //$this->isTest();
    }

    /**
     * 权限验证
     */
    private function isTest()
    {
        if(in_array($this->action, ['delete','status']) || ($this->request->isPost() && in_array($this->action, ['add', 'edit', 'renew']))){
            die(json_encode(['code' => 0, 'msg' => '体验用户，无权操作！'],JSON_UNESCAPED_UNICODE));
        }
    }

    /**
     * 获取当前用户信息
     */
    protected function getUserDetail()
    {
        // 控制器名称
        $this->controller = uncamelize($this->request->controller());
        // 方法名称
        $this->action = $this->request->action();
        // 控制器分组 (用于定义所属模块)
        $group = strstr($this->controller, '.', true);
        $this->group = $group !== false ? $group : $this->controller;
        // 当前uri
        $this->routeUri = "{$this->controller}/$this->action";

        // 验证当前请求是否在白名单
        if (in_array($this->routeUri, $this->allowAllAction)) {
            return false;
        }
        if (!$token = $this->request->param('token')) {
            die(json_encode(['code' => -1, 'msg' => '缺少必要的参数：token']));
        }
        if (!$user = ShopClerkModel::getDetail($token)) {
            die(json_encode(['code' => -1, 'msg' => '没有找到用户信息']));
        }
        $this->user = $user;
    }

     /**
     * 返回封装后的 API 数据到客户端
     */
    protected function renderJson($code = self::JSON_SUCCESS_STATUS, $msg = '', $data = [])
    {
        return json(compact('code', 'msg', 'data'));
    }

    /**
     * 返回操作成功json - 有返回值
     */
    protected function renderSuccess($data = [], $msg = 'success')
    {
        return $this->renderJson(self::JSON_SUCCESS_STATUS, $msg, $data);
    }
    
    /**
     * 返回操作成功json - 无返回值
     */
    protected function renderMsg($msg = 'success')
    {
        return $this->renderJson(self::JSON_SUCCESS_STATUS, $msg);
    }

    /**
     * 返回操作失败json
     */
    protected function renderError($msg = 'error', $data = [])
    {
        return $this->renderJson(self::JSON_ERROR_STATUS, $msg, $data);
    }

    /**
     * 获取post数据 (数组)
     */
    protected function postData($key = null)
    {
        return $this->request->post(is_null($key) ? '' : $key . '/a');
    }

    /**
     * 获取post数据 (数组)
     * @param $key
     * @return mixed
     */
    protected function postForm($key = 'form')
    {
        return $this->postData($key);
    }    
}
